#!/usr/bin/python
import dbus
import gobject # needed to loop & monitor
import time
import threading

# necessary to connect to bus
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

bus = dbus.SessionBus()
proxy = bus.get_object('im.pidgin.plugin.dbus', '/im/pidgin/plugin/dbus')
interface = dbus.Interface(proxy,'inkboard.pidgin.dbus')

cur_tid_invitor = 0 
cur_tid_acceptor = 0 


def registeredTalkCallback( *args, **keywords ):
    app = args[0] 
    tid = int( args[1] )
    #contact = interface.getContact( tid )

    print "registered talk: ", tid, " type: ", app

    global cur_tid_invitor
    cur_tid_invitor = tid


def recvTalkCallback( *args, **keywords ):
    app = args[0] 
    tid = int( args[1] )
    #contact = interface.getContact( tid )

    print "receiv talk: ", tid, " type: ", app

    global cur_tid_acceptor
    cur_tid_acceptor = tid


def recvMessageCallback( *args, **keywords ):
    tid = int( args[0] )
    msg = unicode( args[1] )

    print tid, msg


bus.add_signal_receiver(recvTalkCallback, 
                        'recvTalk', 
                        'inkboard.pidgin.dbus',
                        'im.pidgin.plugin.dbus', 
                        '/im/pidgin/plugin/dbus')

bus.add_signal_receiver(registeredTalkCallback, 
                        'registeredTalk', 
                        'inkboard.pidgin.dbus',
                        'im.pidgin.plugin.dbus', 
                        '/im/pidgin/plugin/dbus')

bus.add_signal_receiver(recvMessageCallback, 
                        'recvMessage', 
                        'inkboard.pidgin.dbus',
                        'im.pidgin.plugin.dbus', 
                        '/im/pidgin/plugin/dbus')



# monitor
loop = gobject.MainLoop()
loop.run()
